//
//  UIButton+Extension.m
//  SaleHouse
//
//  Created by 陈胜华 on 16/11/3.
//  Copyright © 2016年 苏军朋. All rights reserved.
//

#import "UIButton+Extension.h"
#import <objc/runtime.h>

@implementation UIButton (Extension)

#pragma mark 添加点击范围扩展
static char topNameKey;
static char rightNameKey;
static char bottomNameKey;
static char leftNameKey;
static char ActionTag;

- (void) setEnlargeEdgeWithTop:(CGFloat) top right:(CGFloat) right bottom:(CGFloat) bottom left:(CGFloat) left{
    objc_setAssociatedObject(self, &topNameKey, [NSNumber numberWithFloat:top], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &rightNameKey, [NSNumber numberWithFloat:right], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &bottomNameKey, [NSNumber numberWithFloat:bottom], OBJC_ASSOCIATION_COPY_NONATOMIC);
    objc_setAssociatedObject(self, &leftNameKey, [NSNumber numberWithFloat:left], OBJC_ASSOCIATION_COPY_NONATOMIC);
}

- (CGRect) enlargedRect
{
    NSNumber* topEdge = objc_getAssociatedObject(self, &topNameKey);
    NSNumber* rightEdge = objc_getAssociatedObject(self, &rightNameKey);
    NSNumber* bottomEdge = objc_getAssociatedObject(self, &bottomNameKey);
    NSNumber* leftEdge = objc_getAssociatedObject(self, &leftNameKey);
    if (topEdge && rightEdge && bottomEdge && leftEdge)
    {
        return CGRectMake(self.bounds.origin.x - leftEdge.floatValue,
                          self.bounds.origin.y - topEdge.floatValue,
                          self.bounds.size.width + leftEdge.floatValue + rightEdge.floatValue,
                          self.bounds.size.height + topEdge.floatValue + bottomEdge.floatValue);
    }
    else
    {
        return self.bounds;
    }
}

- (UIView*) hitTest:(CGPoint) point withEvent:(UIEvent*) event
{
    CGRect rect = [self enlargedRect];
    if (CGRectEqualToRect(rect, self.bounds))
    {
        return [super hitTest:point withEvent:event];
    }
    return CGRectContainsPoint(rect, point) ? self : nil;
}


- (void)setupEdgeInsetsByDirection:(DFButtonImageDirection)direction{
    
    switch (direction) {
        case DFButtonImageDirectionLeft:
            return;
            break;
        case DFButtonImageDirectionRight:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0, self.titleLabel.intrinsicContentSize.width, 0, -self.titleLabel.intrinsicContentSize.width);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView.frame.size.width, 0, self.imageView.intrinsicContentSize.width);
        }
            break;
        case DFButtonImageDirectionCenter:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(0, self.titleLabel.intrinsicContentSize.width, 0, 0);
            self.titleEdgeInsets = UIEdgeInsetsMake(0, -self.imageView.frame.size.width, 0,0);
            
        }
            break;
        case DFButtonImageDirectionBottom:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(-self.titleLabel.intrinsicContentSize.height, self.titleLabel.intrinsicContentSize.width, 0, 0);
            self.titleEdgeInsets = UIEdgeInsetsMake(self.imageView.frame.size.height, -self.imageView.frame.size.width, 0,0);
        }
            break;
        case DFButtonImageDirectionTop:
        {
            self.imageEdgeInsets = UIEdgeInsetsMake(self.titleLabel.intrinsicContentSize.height, self.titleLabel.intrinsicContentSize.width, 0, 0);
            self.titleEdgeInsets = UIEdgeInsetsMake(-self.imageView.frame.size.height, -self.imageView.frame.size.width, 0,0);
        }
            break;
            
        default:
            break;
    }    
}

//
- (void)addAction:(ButtonBlock)block {
    objc_setAssociatedObject(self, &ActionTag, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
}

- (void)addAction:(ButtonBlock)block forControlEvents:(UIControlEvents)controlEvents {
    objc_setAssociatedObject(self, &ActionTag, block, OBJC_ASSOCIATION_COPY_NONATOMIC);
    [self addTarget:self action:@selector(action:) forControlEvents:controlEvents];
}

- (void)action:(id)sender {
    ButtonBlock blockAction = (ButtonBlock)objc_getAssociatedObject(self, &ActionTag);
    if (blockAction) {
        blockAction(self);
    }
}

@end
